<template>
  <div class="box">
    <el-table
    :data="tableData.slice((this.page.pagenum - 1) * this.page.pagesize, this.page.pagenum * this.page.pagesize)"
    >
      <el-table-column prop="header" label="文章"  align="center"></el-table-column>
      <el-table-column prop="date" label="发布日期"  align="center">
        <template slot-scope="scope">
          {{ scope.row.date | dateChage }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="zan" label="点赞"  align="center"></el-table-column>
      <el-table-column prop="pingnum" label="评论"  align="center"></el-table-column> -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- 编辑 -->
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="edit(scope.row)"></el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleted(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync='page.pagenum'
      :page-size="page.pagesize"
      layout="total, prev, pager, next"
      :total='tableData.length'>
    </el-pagination>
  </div>
</template>

<script>
import http from '@/util/http'
import { Message } from 'element-ui'
export default {
  mounted () {
    this.render()
  },
  data () {
    return {
      tableData: [],
      info: {
        token: window.localStorage.getItem('token'),
        header: ''
      },
      page: {
        query: '',
        pagenum: 1,
        pagesize: 4
      }
    }
  },
  methods: {
    edit (obj) {
      this.info.header = obj.header
      this.$router.push({ name: 'edit', params: { info: this.info } })
    },
    deleted (obj) {
      this.info.header = obj.header
      http.delete('/api/art/updata', { params: this.info }).then(res => {
        if (res.data.code === 1) {
          this.render()
          Message.success('文章删除成功')
        }
      })
    },
    render () {
      http.get('/api/art/info', { params: this.info }).then(res => {
        this.tableData = res.data.info
      })
    },
    handleSizeChange (index) {
      console.log(index)
    },
    handleCurrentChange (index) {
      console.log(index)
    }
  }
}
</script>

<style lang="scss" scoped>
.box{
  margin-top:7.5rem ;
}
.el-table{
  background-color: white;
}
</style>
